package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sikaryofficial.backend.domain.entity.MyStatistics;
import com.sikaryofficial.backend.domain.vo.MyStatisticsVO;
import com.sikaryofficial.backend.domain.vo.ObjectCount;
import com.sikaryofficial.backend.domain.vo.OtherStatisticsVO;
import com.sikaryofficial.backend.mapper.MyStatisticsMapper;
import com.sikaryofficial.backend.service.IMyStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 我的统计Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-06
 */
@Service
public class MyStatisticsServiceImpl extends ServiceImpl<MyStatisticsMapper, MyStatistics> implements IMyStatisticsService {
    @Autowired
    private MyStatisticsMapper myStatisticsMapper;
    @Value("${hismk.backend.mystatistics.batchSize:500}")
    private int batchSize;

    /**
     * 查询个人统计数据
     *
     * @param userId
     * @return
     */
    @Override
    public MyStatisticsVO queryMyStatistics(Long userId) {
        if (Objects.isNull(userId)) {
            return null;
        }
        return myStatisticsMapper.queryMyStatistics(userId);
    }

    @Override
    public OtherStatisticsVO queryOtherStatistics(Long userId) {
        if (Objects.isNull(userId)) {
            return null;
        }
        return myStatisticsMapper.queryOtherStatistics(userId);
    }

    /**
     * 根据对象id查询统计
     *
     * @param objectIds 对象id集合
     * @return
     */
    @Override
    public List<ObjectCount> countByObjectIds(List<Long> objectIds) {
        if (CollUtil.isEmpty(objectIds)) {
            return Collections.emptyList();
        }
        // 分批批量查询
        List<List<Long>> partitions = Lists.partition(objectIds, batchSize);
        List<ObjectCount> result = new ArrayList<>();
        for (List<Long> partition : partitions) {
            result.addAll(myStatisticsMapper.countByObjectIds(partition));
        }
        return result;
    }

    /**
     * 单个对象的用户行为：根据MyStatisticsTypeEnum.businessType 分组
     *
     * @param userId
     * @param objectId 对象id集合
     * @return key: businessType value: ObjectCount
     */
    @Override
    public Map<Integer, ObjectCount> myCountMapByObjectIds(Long userId, Long objectId) {
        if (Objects.isNull(userId) || Objects.isNull(objectId)) {
            return Maps.newHashMap();
        }
        List<Long> objectIds = new ArrayList<>();
        objectIds.add(objectId);
        List<ObjectCount> objectCountList = myStatisticsMapper.myCountByObjectIds(userId, objectIds);
        if (CollUtil.isEmpty(objectCountList)) {
            return Maps.newHashMap();
        }
        return objectCountList.stream().collect(Collectors.toMap(ObjectCount::getBusinessType, Function.identity(), (k1, k2) -> k1));
    }
}
